﻿#region 头注释
/******************************************************************************
文件名称    :   SurgeryHardtube.cs
创建日期    :   2011/5/11 22:19:49
功能描述    :   SurgeryHardtube实体类
修改日志    :   
    2011-05-11 22:19  马利
*******************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
using Dare.Utilities.Data;

namespace Stone.Components.Entities
{
    /// <summary>
    /// SurgeryHardtube实体类
    /// </summary>
    public class SurgeryHardtube : EntityBase<SurgeryHardtube, long>
    {
        #region 构造函数

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public SurgeryHardtube()
            : base()
        {
            base.xmlSerializer = new DefaultXmlSerializer(this);
            base.jsonSerializer = new DefaultJsonSerializer(this);
        }

        public override void Reset()
        {
            slit = null;
            narrow = null;
            narrowPosition = null;
            comment = null;
            timeStamp = DateTime.MinValue;
        }

        public override SurgeryHardtube CreateNew()
        {
            return new SurgeryHardtube();
        }

        #endregion

        #region 属性

        /// <summary>
        /// 获取实体类型
        /// </summary>
        public override EntityType EntityType
        {
            get { return EntityType.SurgeryHardtube; }
        }

        /// <summary>
        /// 获取或设置输尿管是否撕裂
        /// </summary>
        public bool? Slit
        {
            get { return slit; }
            set
            {
                if (entityState == EntityState.Saved && slit != value) entityState = EntityState.Changed;
                slit = value;
            }
        }
        protected bool? slit;

        /// <summary>
        /// 获取或设置输尿管是否狭窄
        /// </summary>
        public bool? Narrow
        {
            get { return narrow; }
            set
            {
                if (entityState == EntityState.Saved && narrow != value) entityState = EntityState.Changed;
                narrow = value;
            }
        }
        protected bool? narrow;

        /// <summary>
        /// 获取或设置输尿管狭窄位置
        /// </summary>
        public string NarrowPosition
        {
            get { return narrowPosition; }
            set
            {
                if (entityState == EntityState.Saved && narrowPosition != value) entityState = EntityState.Changed;
                narrowPosition = value;
            }
        }
        protected string narrowPosition;

        /// <summary>
        /// 获取或设置备注
        /// </summary>
        public string Comment
        {
            get { return comment; }
            set
            {
                if (entityState == EntityState.Saved && comment != value) entityState = EntityState.Changed;
                comment = value;
            }
        }
        protected string comment;

        /// <summary>
        /// 获取或设置更新时间戳
        /// </summary>
        public DateTime TimeStamp
        {
            get { return timeStamp; }
            set
            {
                if (entityState == EntityState.Saved && timeStamp != value) entityState = EntityState.Changed;
                timeStamp = value;
            }
        }
        protected DateTime timeStamp;

        #endregion

        #region 方法

        #endregion

        #region 实体复制相关方法

        /// <summary>
        /// 复制当前实体属性到指定实体上
        /// </summary>
        /// <param name="entity">要把属性复制到的实体</param>
        /// <returns>返回传入的实体</returns>
        public override SurgeryHardtube CopyTo(SurgeryHardtube entity)
        {
            if (base.CopyTo(entity) == null) return null;

            entity.slit = this.slit;
            entity.narrow = this.narrow;
            entity.narrowPosition = this.narrowPosition;
            entity.comment = this.comment;
            entity.timeStamp = this.timeStamp;

            return entity;
        }

        #endregion

        #region 默认XML序列器类

        public class DefaultXmlSerializer : XmlSerializerBase, IEntityXmlSerializer<SurgeryHardtube>
        {
            SurgeryHardtube entity;
            public DefaultXmlSerializer(SurgeryHardtube entity)
                : base(entity.GetType().Name)
            {
                this.entity = entity;
            }

            public SurgeryHardtube Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            protected override void BeforeReadXml()
            {
                entity.Reset();
            }

            /// <summary>
            /// 读取XML属性
            /// </summary>
            /// <param name="name">属性名</param>
            /// <param name="value">属性值</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlAttribute(string name, string value, XmlReader reader)
            {
                switch (name)
                {
                    case "id":
                        entity.id = XmlConvert.ToInt64(value);
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(long.Parse(value));
                        break;
                    case "entityState":
                        entity.entityState = (EntityState)Enum.Parse(typeof(EntityState), value, true);
                        break;
                }
            }

            /// <summary>
            /// 读取XML子节点
            /// </summary>
            /// <param name="name">节点名</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlChildElement(string name, XmlReader reader)
            {
                switch (name)
                {
                    case "Slit":
                        entity.slit = XmlConvert.ToBoolean(reader.ReadElementString());
                        break;
                    case "Narrow":
                        entity.narrow = XmlConvert.ToBoolean(reader.ReadElementString());
                        break;
                    case "NarrowPosition":
                        entity.narrowPosition = reader.ReadElementString();
                        break;
                    case "Comment":
                        entity.comment = reader.ReadElementString();
                        break;
                }
            }

            /// <summary>
            /// 写入XML属性
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlAttributes(XmlWriter writer)
            {
                writer.WriteAttributeString("id", XmlConvert.ToString(entity.Id));
                writer.WriteAttributeString("timeStamp", entity.TimeStamp.Ticks.ToString());
                writer.WriteAttributeString("entityState", entity.EntityState.ToString());
            }

            /// <summary>
            /// 写入XML子节点
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlChildElements(XmlWriter writer)
            {
                if (entity.Slit != null) writer.WriteElementString("Slit", XmlConvert.ToString(entity.Slit.Value));
                if (entity.Narrow != null) writer.WriteElementString("Narrow", XmlConvert.ToString(entity.Narrow.Value));
                if (entity.NarrowPosition != null) writer.WriteElementString("NarrowPosition", entity.NarrowPosition);
                if (entity.Comment != null) writer.WriteElementString("Comment", entity.Comment);
            }
        }

        #endregion

        #region 默认Json序列化器类

        public class DefaultJsonSerializer : JsonSerializerBase, IEntityJsonSerializer<SurgeryHardtube>
        {
            SurgeryHardtube entity;
            public DefaultJsonSerializer(SurgeryHardtube entity)
                : base()
            {
                this.entity = entity;
            }

            public SurgeryHardtube Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            /// <summary>
            /// 读取JSON属性
            /// </summary>
            /// <param name="propName">属性名称</param>
            /// <param name="propValue">属性值</param>
            /// <param name="reader">JSON读取器</param>
            protected override void ReadJsonProperty(string propName, object propValue, JsonReader reader)
            {
                switch (propName)
                {
                    case "id":
                        entity.id = DbConvert.ToNullableInt64(propValue) ?? entity.id;
                        break;
                    case "slit":
                        entity.slit = DbConvert.ToNullableBoolean(propValue) ?? entity.slit;
                        break;
                    case "narrow":
                        entity.narrow = DbConvert.ToNullableBoolean(propValue) ?? entity.narrow;
                        break;
                    case "narrowPosition":
                        entity.narrowPosition = Convert.ToString(propValue) ?? entity.narrowPosition;
                        break;
                    case "comment":
                        entity.comment = Convert.ToString(propValue) ?? entity.comment;
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(DbConvert.ToNullableInt64(propValue) ?? 0);
                        break;
                }
            }

            /// <summary>
            /// 写入JSON属性
            /// </summary>
            /// <param name="writer">JSON编写器</param>
            protected override void WriteJsonProperties(JsonWriter writer)
            {

                //Id
                writer.WritePropertyName("id");
                writer.WriteValue(entity.Id);

                //输尿管是否撕裂
                writer.WritePropertyName("slit");
                if (entity.Slit == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Slit.Value);
                }

                //输尿管是否狭窄
                writer.WritePropertyName("narrow");
                if (entity.Narrow == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Narrow.Value);
                }

                //输尿管狭窄位置
                writer.WritePropertyName("narrowPosition");
                if (entity.NarrowPosition == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.NarrowPosition);
                }

                //备注
                writer.WritePropertyName("comment");
                if (entity.Comment == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Comment);
                }

                //更新时间戳
                writer.WritePropertyName("timeStamp");
                writer.WriteValue(entity.TimeStamp.Ticks);
            }
        }


        #endregion
    }
}